package com.example.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.manager.BlockManager;
import com.example.manager.GameCenter;
import com.example.model.Block;
import com.example.utils.Constants;

public class GameView extends View {
    private int row = Constants.ROW;
    private int column = Constants.COLUMN;
    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int leftMargin = 0;
    private int topMargin = 0;


    public GameView(Context context) {
        this(context,null);
    }

    public GameView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

//        int mWidth = Constants.COLUMN * Constants.SIZE;
//        int mHeight = Constants.ROW * Constants.SIZE;
//
//        int realWidth = resolveSizeAndState(mWidth,widthMeasureSpec,MEASURED_STATE_MASK);
//        int realHeight = resolveSizeAndState(mHeight,heightMeasureSpec,MEASURED_STATE_MASK);
//
//        row = realHeight / Constants.SIZE;
//        column = realWidth / Constants.SIZE;

        int realWidth;
        int realHeight;

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY){
            realWidth = widthSize;
        }else {
            realWidth = Constants.COLUMN * Constants.SIZE;
        }

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY){
            realHeight = heightSize;
        }else {
            realHeight = Constants.ROW * Constants.SIZE;
        }

        row = realHeight / Constants.SIZE;
        column = realWidth / Constants.SIZE;


        setMeasuredDimension(realWidth,realHeight);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //单独计算边距让游戏界面显示在中心位置
        leftMargin = ((getWidth() - column * (Constants.SIZE)) / 2);
        topMargin = ((getHeight() - row * (Constants.SIZE)) / 2);

        //将测量之后的值给gameCenter
        GameCenter.defaultCenter.setBoardSize(row,column);

    }

    public void refresh(){
        invalidate();
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        //绘制背景方块
        drawBackgroundGrid(canvas);
        //绘制当前图形
        drawCurrentBlock(canvas);
        //绘制面板中固定的方块
        drawFixedBlock(canvas);
    }

    private void drawFixedBlock(Canvas canvas) {
        int[][] boards = GameCenter.defaultCenter.boards;
        Rect rect = new Rect();
        for (int i = 0; i < boards.length; i++) {
            for (int j = 0; j < boards[0].length; j++) {
                if (boards[i][j] == 1){

                    rect.left = j * Constants.SIZE + leftMargin;
                    rect.top = i * Constants.SIZE  + topMargin;
                    rect.right = rect.left + Constants.SIZE;
                    rect.bottom = rect.top + Constants.SIZE;

                    canvas.drawBitmap(Constants.getDefaultBitmap(getContext()),null,rect,null);
                }
            }
        }
    }

    private void drawCurrentBlock(Canvas canvas){
        //获取到方块对象和其对应的二维数组，后续遍历数组绘制相应的图形
        Block block = BlockManager.defaultManager.getCurrentBlock();
        int[][]shapes = block.getShape();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(),block.imageResource);
        Rect rect = new Rect();
        for (int i = 0; i < shapes.length; i++) {
            for (int j = 0; j < shapes[0].length; j++) {
                if (shapes[i][j] == 1){

                    rect.left = block.column * Constants.SIZE + j * Constants.SIZE + leftMargin;
                    rect.top = block.row * Constants.SIZE + i * Constants.SIZE + topMargin;
                    rect.right = rect.left + Constants.SIZE;
                    rect.bottom = rect.top + Constants.SIZE;

                    canvas.drawBitmap(bitmap,null,rect,null);
                }

            }
        }
    }

    //绘制背景方格
    private void drawBackgroundGrid(Canvas canvas){

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                //画笔颜色
                if ((i + j) %2 == 0){
                   // mPaint.setColor(Color.parseColor("#FFC0CB"));
                    mPaint.setColor(Color.parseColor("#121212"));
// 格式：#AARRGGBB，AA=透明度（80=50%），RRGGBB=FFFFFF（纯白）
                }else {
                    //mPaint.setColor(Color.parseColor("#E6E6FA"));
                    mPaint.setColor(Color.parseColor("#0A0A0A"));
                    //mPaint.setColor(Color.parseColor("#000000"));
// 20=12.5%透明度
                }

                canvas.drawRect(j * Constants.SIZE + leftMargin, i * Constants.SIZE + topMargin, (j + 1) * Constants.SIZE + leftMargin, (i + 1) * Constants.SIZE + topMargin,mPaint);
            }
        }
    }
}
